package com.mordenx.action;

import com.mordenx.entity.SecKillingUser;
import com.mordenx.service.SecKillingUserService;
import com.mordenx.vo.user.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("secKillingUserAction")
public class SecKillingUserAction {
    @Autowired
    SecKillingUserService secKillingUserService;

    @RequestMapping("addUser")
    public  String addMerchant(){
        return "user/addUser";
    }

    @RequestMapping(value = "add",method = RequestMethod.POST)
    public String insertMerchant(SecKillingUser secKillingUser){
        secKillingUserService.insertUser(secKillingUser);
        return "redirect:queryByVo";
    }

    @RequestMapping("delete")
    public String deleteUser( int id){
        secKillingUserService.deleteUserById(id);
        return "redirect:queryByVo";
    }

    @RequestMapping("update")
    public String updateUser(HttpServletRequest httpServletRequest, int id){
        SecKillingUser user
                = secKillingUserService.queryById(id);
        httpServletRequest.setAttribute("user",user);
        return "user/updateUser";
    }

    @RequestMapping(value = "toUpdate",method = RequestMethod.POST)
    public String updateUserById(HttpServletRequest httpServletRequest, SecKillingUser secKillingUser){
        System.out.println(secKillingUser.toString());
        secKillingUserService.updateUserById(secKillingUser);
        httpServletRequest.setAttribute("user",secKillingUser);
        return "redirect:queryByVo";
    }

    @RequestMapping("query")
    public String queryById(HttpServletRequest httpServletRequest,int id){
        SecKillingUser secKillingUser= secKillingUserService.queryById(id);
        httpServletRequest.setAttribute("user",secKillingUser);
        return "user/viewUser";
    }

    @RequestMapping("queryByVo")
    public String queryByVo(HttpServletRequest httpServletRequest, UserVo userVo){
        List<SecKillingUser> userList =secKillingUserService.queryUserByVo(userVo);
        httpServletRequest.setAttribute("userList",userList);
        return "user/userList";
    }

}
